package com.hpeng.gl.action;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.basic.service.ProdStatusMstManager;
import com.hpeng.sys.model.ProdStatusMst;
import com.opensymphony.xwork2.ActionSupport;
@Component("prodStatusMstOutputExcelAction")
@Scope("prototype")  
public class ProdStatusMstOutputExcelAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;
	
	@Resource
	// 资产状态
	private ProdStatusMstManager prodStatusMstManager;

	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	// 获取输出流
	public InputStream getInputExcel() {
		return (InputStream) request.getAttribute("excelStream");
	}

	@Override
	public String execute() {
		List<ProdStatusMst> list = prodStatusMstManager.findProdStatusMst();
		String[] columnNames = {"资产主状态编号","资产主状态名称","资产从状态编号","资产从状态名称","资产状态使用情况"};
		
		if (list != null) {
			int rows = 0;
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			WritableWorkbook book;
			try {
				//避免乱码
				WorkbookSettings setting = new WorkbookSettings();
			    Locale local = new Locale("zh","CN");
			    setting.setLocale(local);
			    setting.setEncoding("UTF-8");
			    Workbook.createWorkbook(os, setting);
			    
				book = Workbook.createWorkbook(os);
				WritableSheet sheet = book.createSheet("第一页", rows); //设置工作薄当前页数
				for (int i = 0; i < list.size(); i++) {
					for(int j=0; j<columnNames.length; j++){
					//Label..第一个参数为列,第二个参数为行,第三个参数为值.
						Label label1 = new Label(j, 0, columnNames[j]);
						sheet.addCell(label1);
					}
					//输出
					Label label2 = new Label(0,i+1,list.get(i).getStatuscode());
					Label label3 = new Label(1,i+1,list.get(i).getStatusdesc());
					Label label4 = null;
					try {
						if(list.get(i).getStatus().equals("Y")){
							   label4 = new Label(4,i+1,"在用");
							}else{
								label4 = new Label(4,i+1,"弃用");
							}
					} catch (Exception e) {
						e.printStackTrace();
					}
					Label label5 = new Label(2,i+1,list.get(i).getSubcode());
					Label label6 = new Label(3,i+1,list.get(i).getSubdesc());
	
					sheet.addCell(label2);
					sheet.addCell(label3);
					sheet.addCell(label4);
					sheet.addCell(label5);
					sheet.addCell(label6);
					
				}
				
				book.write();
				book.close();
				request.setAttribute("excelStream", new ByteArrayInputStream(os
						.toByteArray()));
				request.setAttribute("filename", "myexcel.xls");//设置文件名 
				return "success";
			} catch (IOException e) {
				e.printStackTrace();
				return "error";
			} catch (RowsExceededException e) {
				e.printStackTrace();
				return "error";
			} catch (WriteException e) {
				e.printStackTrace();
				return "error";
			}
		}else{
			System.out.println("统计数据不存在");
			return "error";
		}
	}
  
}
